const http = require("http");
let baseUrl = "http://localhost:3000";
let login = require("./login");
let user = require("./user");
http.createServer((req, res) => {
    let urlObj = new URL(baseUrl + req.url);
    if(urlObj.pathname == "/" && req.method == "GET") {
        let result = login.readLogin();
        res.end(result);
    }
    if(urlObj.pathname == "/test" && req.method == "POST") {
        let str = "";
        req.on("data", (chunk) => {
            str += chunk;
        })
        req.on("end", () => {
            userObj = JSON.parse(str);
            let userName = userObj.userName;
            let pwd = userObj.pwd;
            if(userName == "zhangsan" && pwd == "123456") {
                res.end(JSON.stringify({"msg": "success"}));
            }
        })
    }
    if(urlObj.pathname == "/list" && req.method == "GET") {
        let result = user.readList();
        /**
         * statuscode 
         * 200服务端响应成功
         * 404资源未找到
         * 500服务端错误
         * 
         */
        res.writeHead(200, {"content-type": "text/html"});
        res.end(result);
    }
}).listen(3000)
console.log("server is listening 3000");